"""
======================
Author: 柠檬班-小简
Time: 2022/10/24 14:16
Project: day4_framwork_V1
Company: 湖南零檬信息技术有限公司
======================
"""
"""
获取验证码图片，url当中的uuid值，
要在登录的时候，传递给请求body当中的SessionUUID参数
"""
# 1、获取验证码图片
import base64
from uuid import uuid4

import requests

s = requests.Session()
uuid = str(uuid4())

url = 'http://mall.lemonban.com:8108/captcha.jpg'
req_params = {
    "uuid":uuid
}
response = s.get(url, params=req_params)

# 2、通过第三方库接口，得到验证码数字
url = "http://api.ttshitu.com/predict"
# 读取图片，并进行base64编码
base64_data = base64.b64encode(response.content)
b64 = base64_data.decode()

# 请求数据
req_data = {
    "username":"simple",
    "password": "yuan5311645",
    "typeid":"3",
    "image":b64
}
resp = s.post(url, json=req_data)
result_dict = resp.json()
image_code = result_dict["data"]["result"]
print(result_dict)
print(image_code)

# 3、登录
url = "	http://mall.lemonban.com:8108/adminLogin"
req_data = {
    "principal":"student",
    "credentials":"123456a",
    "imageCode":image_code,
    "sessionUUID": uuid
    }
resp = s.post(url,json=req_data)
print(resp.text)